public class SearchRange {
    // lletcode 在排序数组中查找元素的第一个和最后一个位置
    // https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/description/?envType=study-plan-v2&envId=top-100-liked
    public int[] searchRange(int[] nums, int target) {
        // 先将数据分为小于和大于等于两部分，找到左边界
        // 再将数据分为小于等于和大于连部分，找到右边界
        if (nums.length == 0) return new int[]{-1,-1};
        int[] ret = new int[2];
        int left = 0, right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            }else {
                right = mid;
            }
        }
        if (nums[left] != target) {
            ret[0] = ret[1] = -1;
            return ret;
        }else {
            ret[0] = left;
        }
        left = 0;
        right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2 + 1;
            if (nums[mid] > target) {
                right = mid - 1;
            }else {
                left = mid;
            }
        }
        ret[1] = left;
        return ret;
    }
}
